//
//  DHBaseListViewController.m
//  DHKuaiYi
//
//  Created by Cedric Zheng on 13/11/2016.
//  Copyright © 2016 30-陈辉华. All rights reserved.
//

#import "DHBaseListViewController.h"
#import "DHListModel.h"

static NSString* listReuseID = @"ListReuseID";

@interface DHBaseListViewController () <UITableViewDataSource>

@end

@implementation DHBaseListViewController


#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView* tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView = tv;
    [self.view addSubview:tv];
    
    tv.dataSource =self;
}

#pragma mark - Events
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark – Private Methods
- (void)loadDataListWithName:(NSString *)plistFileName {
    //用URL获取plist文件里的数组
    NSURL* url = [[NSBundle mainBundle] URLForResource:plistFileName withExtension:nil];
    NSArray* plistArr = [NSArray arrayWithContentsOfURL:url];
    
    //遍历数组，把字典转换模型
    NSMutableArray* dataArrM = [NSMutableArray arrayWithCapacity:plistArr.count];
    for (NSArray* dictArr in plistArr) {
        
        NSMutableArray* groupArrM = [NSMutableArray arrayWithCapacity:dictArr.count];
        for (NSDictionary* dict in dictArr) {
            DHListModel* model = [DHListModel new];
            [model setValuesForKeysWithDictionary:dict];
            [groupArrM addObject:model];
        }
        
        [dataArrM addObject:groupArrM];
    }
    //保存
    self.dataList = [dataArrM copy];
    
    //TableView重新加载数据
    [self.tableView reloadData];
}

#pragma mark - <#Delegates#>

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray* arr = _dataList[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //返回一个样式的cell
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:listReuseID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:listReuseID];
    }
    
    //取出DataList里的数据
    NSArray* dataArr = self.dataList[indexPath.section];
    DHListModel* model = dataArr[indexPath.row];
    
    //为cell赋值
    cell.textLabel.text = model.name;
    //判断有没有图片
    if (model.icon != nil) {
        cell.imageView.image = [UIImage imageNamed:model.icon];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //判断有没有detail
    if (model.detail != nil) {
        cell.detailTextLabel.text = model.detail;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

#pragma mark – Getters and Setters

@end
